package com.sakura.demo2.gateway.handler;

import com.sakura.demo.common.error.SystemErrorType;
import com.sakura.demo2.gateway.utils.WebfluxResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: zhengcan
 * @Date: 2022/5/13
 * @Description: 自定义返回结果：没有登录或token过期时
 * @Version: 1.0.0 创建
 */
@Slf4j
@Component
public class RestAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
    @Override
    public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
        log.error("[RestAuthenticationEntryPoint]：token验证失败，e={}", e);
        return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), SystemErrorType.UNAUTHORIZED);
    }
}
